package com.itbaima.service.impl;

import com.itbaima.exception.AccountNotFoundException;
import com.itbaima.exception.PasswordErrorException;
import com.itbaima.mapper.UserMapper;
import com.itbaima.message.MessageConstant;
import com.itbaima.pojo.entity.User;
import com.itbaima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {

        String username = user.getUsername();
        String password = user.getPassword();

        //根据用户名查询数据库中的数据 ， 用于比对的角色信息
        User user2 = userMapper.getByUsername(username);

        //假设用户名未查询到
        if(user2 == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码错误
        if(!password.equals(user2.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return user2 ;


    }

    @Override
    public User getUserByUsername(String username) {
        // 调用数据访问层的方法，根据用户名查询用户信息
        return userMapper.findByUsername(username);
    }
}
